import type { AjaxPagingResult, CarbonInventory, CarbonInventoryActivityDetailsType, FacilityActivityData, SaveOrUpdateCarbonInventoryActivityBodyType } from '@/types'

const controller = getAjaxBaseUrl('/carbonInventory', '/energy')

/** 查询盘查信息（含活动） */
export function queryCarbonInventory(params: {
  /** 核算主体 */
  pointId: number
  /** 核算年份 */
  dataTime: number
  /** 核算协议 */
  protocolCode: string
}) {
  return get(`${controller}/facilityActivity/getList`, params)
}

/** 新增子类别 */
export function addCarbonInventorySubCategory(params: {
  /** 核算主体 */
  pointId: number
  /** 核算年份 */
  dataTime: number
  /** 核算协议 */
  protocolCode: string
  /** 类别名称 */
  name: string
  /** 父类别id */
  pid: number
}) {
  return post<{
    id: number
    name: string
    xh: string
    carbonInventoryId: number
  }>(`${controller}/category/save`, params)
}

/** 删除子类别 */
export function deleteCarbonInventorySubCategory(params: {
  /** 盘查id */
  carbonInventoryId: number
  /** 类别id */
  categoryId: number
}) {
  return get(`${controller}/category/delete`, params)
}

/** 分页查询盘查信息列表 */
export function queryCarbonInventoryList(
  params: {
    pageNumber: number
    pageSize: number
    pointId: number
    protocolCode?: string
    dataTime?: number | string
  },
) {
  return post<AjaxPagingResult<CarbonInventory[]>>(
    `${controller}/page`,
    params,
  )
}

/** 删除盘查信息 */
export function deleteCarbonInventoryById(carbonInventoryId: number) {
  return get(`${controller}/del`, { carbonInventoryId })
}

/** 撤销盘查归档 */
export function cancelCarbonInventoryById(carbonInventoryId: number) {
  return get(`${controller}/recallArchive`, { carbonInventoryId })
}

/** 归档盘查信息 */
export function archiveCarbonInventoryById(carbonInventoryId: number) {
  return get(`${controller}/archive`, { carbonInventoryId })
}

/** 获取温室气体列表 */
export function queryCarbonInventoryGasList() {
  return get<{ id: number, gas: string, gasName: string, gwp: number }[]>(`${controller}/gasGwp/list`)
}

/** 保存设施/活动信息 */
export function saveOrUpdateCarbonInventoryActivity(data: SaveOrUpdateCarbonInventoryActivityBodyType) {
  return post(`${controller}/facilityActivity/save`, data)
}

/** 查询活动详情 */
export function queryCarbonInventoryActivityDetailsById(id: number) {
  return get<CarbonInventoryActivityDetailsType>(`${controller}/facilityActivity/details`, { id })
}

/** 删除设施/活动 */
export function deleteCarbonInventoryActivityById(id: number) {
  return get(`${controller}/facilityActivity/del`, { id })
}

/** 盘查另存为模板 */
export function saveAsTplWithCarbonInventory(name: string, carbonInventoryId: number) {
  return get(`${controller}/template/save`, {
    name,
    carbonInventoryId,
  })
}

/** 分页查询模板列表 */
export function queryTplByPage(params: {
  pageNumber: number
  pageSize: number
  name?: string
  protocolCode?: string
  pointId?: number
}) {
  return post<AjaxPagingResult<any>>(`${controller}/template/page`, params)
}

/** 判断是否能在指定年份应用指定模板 */
export function checkCanUseTemplate(params: {
  pointId: number
  protocolCode: string
  dataTime: number
}) {
  return get(`${controller}/count`, params)
}

/** 使用模板ID创建新盘查 */
export function createCarbonInventoryByTemplateId(params: {
  templateId: number
  dataTime: number
}) {
  return get(`${controller}/saveByTemplate`, params)
}

/** 根据模板ID查盘查快照 */
export function loadCarbonInventorySnapshotByTemplateId(id: number) {
  return get(`${controller}/template/getList`, { id })
}

/** 删除模板 */
export function deleteCarbonInventoryTemplateById(id: number) {
  return get(`${controller}/template/del`, { id })
}

/** 查询盘查的数据录入信息 */
export function queryCarbonInventoryDataEntryInfo(carbonInventoryId: number) {
  return get<FacilityActivityData[]>(`${controller}/facilityActivityData/list`, { carbonInventoryId })
}

/** 保存盘查数据录入信息 */
export function saveCarbonInventoryDataEntryInfo(data: FacilityActivityData[]) {
  return post(`${controller}/facilityActivityData/save`, data)
}

/** 盘查活动数据凭证文件上传 */
export function uploadCarbonInventoryDataEntryFile(
  activityId: number,
  file: File,
) {
  const formData = new FormData()
  formData.append('file', file)
  return post<{ id: number, fileName: string, fileUrl: string }>(
    `${controller}/facilityActivityData/upload?activityId=${activityId}`,
    formData,
    {
      'Content-Type': 'multipart/form-data',
    },
  )
}

/** 查询盘查活动附件列表 */
export function queryCarbonInventoryDataEntryFileList(activityId: number) {
  return get<{ id: number, fileName: string, fileUrl: string }[]>(
    `${controller}/facilityActivityData/fileList`,
    { activityId },
  )
}

/**
 * 删除盘查活动附件
 */
export function deleteCarbonInventoryDataEntryFileById(id: number) {
  return get(`${controller}/facilityActivityData/fileDel`, { id })
}

/** 获取盘查工业总产值数据 */
export function queryCarbonInventoryIndustryTotalValueByCarbonInventoryId(
  carbonInventoryId: number,
) {
  return get(`${controller}/getGrossIndustrialOutputValue`, { carbonInventoryId })
}

/** 保存盘查工业总产值数据 */
export function saveCarbonInventoryIndustryTotalValue(params: {
  carbonInventoryId: number
  grossIndustrialOutputValue: number
}) {
  return get(`${controller}/saveGrossIndustrialOutputValue`, params)
}

/** 计算盘查核算汇总 */
export function calculateCarbonInventorySummary(carbonInventoryId: number) {
  return get(`${controller}/summary`, { carbonInventoryId })
}

/** 盘查数据预览 */
export function previewCarbonInventoryDetails(carbonInventoryId: number) {
  return get(`${controller}/previewDetails`, { carbonInventoryId })
}
